from django.db import models
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from application import dispatch
from dvadmin.utils.models import CoreModel, table_prefix, get_custom_app_models
from dvadmin.system.models import Users

# 投放日志
class InputLogModel(CoreModel):
    input_weight = models.CharField(verbose_name="投放重量",max_length=255)
    input_before_weight = models.CharField(verbose_name="投放前重量",max_length=255,null=True,blank=True)
    input_time = models.DateTimeField(verbose_name="投放时间",auto_now_add=True)
    wxuser = models.ForeignKey(to="WechatUserModel",related_name='wechat_user',on_delete=models.CASCADE,default="",null=True,blank=True)
    video_path = models.CharField(verbose_name="录像路径",max_length=255)
    device = models.ForeignKey(to="DeviceModel",related_name='input_history',on_delete=models.CASCADE,null=True,blank=True)
    price = models.CharField(verbose_name='价格',max_length=255,default=0,null=True,blank=True)
    user_score = models.CharField(verbose_name="用户积分",max_length=255,default=0)
    user_pre_score = models.CharField(verbose_name="用户预积分",max_length=255,default=0)
    bag_code = models.ForeignKey(to="BagCodeModel",related_name='clean_history',on_delete=models.CASCADE,null=True,blank=True)
    # 分账
    sharing_score = models.DecimalField(max_digits=10,decimal_places=2,default=0.00,null=True)
    # 已入账
    sharing_score_checked = models.DecimalField(max_digits=10,decimal_places=2,default=0.00,null=True)
    STATUS_TYPE = (
        (0, "异常"),
        (1, "正常"),
        (2, "违规")
    )
    STATUS_ERROR = (
          (0,"待处理"),
          (1,"已处理")
    )
    status = models.IntegerField(verbose_name="状态",choices=STATUS_TYPE,default=1,help_text="投放状态")
    error_status = models.IntegerField(verbose_name="违规状态",choices=STATUS_ERROR,default=0,help_text="违规状态")
    mark = models.CharField(verbose_name="备注",max_length=255,default='')
    clean_history = models.ForeignKey(to="CleanLogModel",related_name='input_history',on_delete=models.CASCADE,null=True,blank=True)
    fake = models.BooleanField(verbose_name="模拟数据",default=False)
    class Meta:
        db_table = "input_log"
        verbose_name = "投放日志"
        verbose_name_plural = verbose_name
        ordering = ["-input_time"]
        

# 设备信息
class DeviceModel(CoreModel):
    device_id = models.CharField(verbose_name="设备ID",max_length=255,unique=True)
    device_mac = models.CharField(verbose_name="设备mac",max_length=255,null=True,blank=True)
    device_name = models.CharField(verbose_name="设备名称",max_length=255,null=True,blank=True)
    device_position = models.CharField(verbose_name="设备位置",max_length=255,null=True,blank=True)
    device_address = models.CharField(verbose_name="设备所在社区",max_length=255,null=True,blank=True)
    STATUS_TYPE = (
        (0, "异常"),
        (1, "正常"),
        (2, "清运中")
    )
    device_status = models.IntegerField(verbose_name="状态",choices=STATUS_TYPE,default=1,help_text="设备状态")
    FULL_STATUS_TYPE = (
          (0,"未满"),
          (1,"已满")
    )
    device_clean_time = models.CharField(verbose_name="清运时间",max_length=255,null=True,blank=True)
    device_full_status = models.IntegerField(verbose_name="满溢状态",choices=FULL_STATUS_TYPE,default=1,help_text="满溢状态")
    device_full_time = models.CharField(verbose_name="满溢时间",max_length=255,null=True,blank=True)
    card_number = models.CharField(verbose_name="卡号",max_length=255,null=True,blank=True)
    card_type = models.CharField(verbose_name="卡运营商",max_length=255,null=True,blank=True)
    device_user = models.ForeignKey(to=Users,related_name='device',blank=True,null=True,on_delete=models.CASCADE)
    admin_user = models.ForeignKey(to=Users,related_name='admin_device',blank=True,null=True,on_delete=models.CASCADE)
    sort_user = models.ForeignKey(to=Users,related_name="sort_device",blank=True,null=True,on_delete=models.CASCADE)
    bag_code = models.ForeignKey(to="BagCodeModel",related_name='device_bag',on_delete=models.CASCADE,null=True,blank=True)
    # sharing_user = models.ForeignKey(to=Users,related_name='sharing',blank=True,null=True,on_delete=models.CASCADE)
    sharing_user = models.CharField(verbose_name='分账手机号',max_length=15,null=True,blank=True)
    class Meta:
            db_table = "device"
            verbose_name = "设备信息"
            verbose_name_plural = verbose_name
            ordering = ["device_id"]
# 微信会员信息
class WechatUserModel(CoreModel):
    openid = models.CharField(verbose_name="微信OpenId",max_length=255,unique=True)
    phone = models.CharField(verbose_name="手机号",max_length=255,unique=True)
    score = models.CharField(verbose_name="当前积分",max_length=255,null=True,blank=True)
    pre_score = models.CharField(verbose_name="预处理积分",max_length=255,null=True,blank=True)
    community = models.CharField(verbose_name="社区",max_length=255,null=True,blank=True)
    # 投放记录
    # input_history = models.ManyToManyField(to=InputLogModel,related_name='input_history',blank=True)
   
    class Meta:
            db_table = "wxchat_user"
            verbose_name = "微信会员信息"
            verbose_name_plural = verbose_name
            ordering = ["phone"]
# 微信扫码记录
class WeChatUserScanModel(CoreModel):
    openid = models.CharField(verbose_name="微信OpenId",max_length=255,null=True,blank=True)
    phone = models.CharField(verbose_name="手机号",max_length=255,null=True,blank=True)
    device_id = models.CharField(verbose_name="设备id",max_length=255)
    class Meta:
            db_table = "scan_log"
            verbose_name = "微信会员扫码记录"
            verbose_name_plural = verbose_name
            ordering = ["-create_datetime"]

# 积分记录
class WxChatUserScoreModel(CoreModel):
    phone = models.CharField(verbose_name="手机号",max_length=255)
    openid = models.CharField(verbose_name="微信OpenId",max_length=255)
    per_score = models.CharField(verbose_name="预计积分",max_length=255,null=True,blank=True)
    score = models.CharField(verbose_name="积分",max_length=255)
    # 积分类型  0减少积分 1添加积分
    score_type = models.CharField(verbose_name="积分类型",max_length=255)
    score_time = models.DateTimeField(verbose_name="积分时间",auto_now_add=True)
    # 积分来源
    score_from = models.CharField(verbose_name="来源",max_length=255,null=True,blank=True)
    # 微信支付的id
    batch_id = models.CharField(verbose_name="支付id",max_length=255,null=True,blank=True)
    out_batch_no = models.CharField(verbose_name="支付out-no",max_length=255,null=True,blank=True)

    class Meta:
                db_table = "score_log"
                verbose_name = "积分记录"
                verbose_name_plural = verbose_name
                ordering = ["-create_datetime"]

# 清运记录
class CleanLogModel(CoreModel):
    user = models.ForeignKey(to=Users,related_name='clean_history',blank=True,null=True,on_delete=models.CASCADE)
    admin_user = models.ForeignKey(to=Users,related_name='admin_clean',blank=True,null=True,on_delete=models.CASCADE)
    sort_user = models.ForeignKey(to=Users,related_name="sort_clean",blank=True,null=True,on_delete=models.CASCADE)
    device = models.ForeignKey(to="DeviceModel",related_name='clean_history',on_delete=models.CASCADE,null=True,blank=True)
    clean_time = models.DateTimeField(verbose_name="清运时间",auto_now_add=True,null=True,blank=True)
    clean_weight = models.CharField(verbose_name="清运重量",max_length=255,null=True,blank=True)
    clean_address = models.CharField(verbose_name="清运地址",max_length=255,null=True,blank=True)
    STATUS_TYPE = (
             (0,"待清运"),
             (1,"正在清运"),
             (2,"已清运待分拣"),
             (3,"正在分拣"),
             (4,"已分拣完成"),
             (9,"异常")
      )
    clean_status = models.IntegerField(verbose_name="清运状态",choices=STATUS_TYPE,default=0,help_text="分拣状态")
    clean_score = models.CharField(verbose_name="清运积分",max_length=255,null=True,blank=True)
    bag_code = models.ForeignKey(to="BagCodeModel",related_name='clean',on_delete=models.CASCADE,null=True,blank=True)
    error_weight = models.CharField(verbose_name="分拣异常重量",max_length=255,null=True,blank=True)
    real_weight = models.CharField(verbose_name="复称重量",max_length=255,null=True,blank=True)
    fake = models.BooleanField(verbose_name="模拟数据",default=False)
    class Meta:
                db_table = "clean_log"
                verbose_name = "清运记录"
                verbose_name_plural = verbose_name
                ordering = ["-create_datetime"]


# 垃圾袋编号
class BagCodeModel(CoreModel):
    bag_code = models.CharField(verbose_name="垃圾袋编号",max_length=255,unique=True)
    STATUS_TYPE = (
             (0,"未录入"),
             (1,"已录入"),
             (2,"使用中"),
             (3,"已破损"),
             (4,"已使用")
      )
    bag_status = models.IntegerField(verbose_name="垃圾袋状态",choices=STATUS_TYPE,default=0,help_text="垃圾袋状态")
    bag_user = models.ForeignKey(to=Users,related_name='bag_user',on_delete=models.CASCADE,null=True,blank=True)
    class Meta:
                db_table = "bag_code"
                verbose_name = "垃圾袋编号"
                verbose_name_plural = verbose_name
                ordering = ["-create_datetime"]


# 分拣记录
class SortModel(CoreModel):
      bag_code = models.ForeignKey(to="BagCodeModel",related_name='sort_bag',on_delete=models.CASCADE,null=True,blank=True)
      user = models.ForeignKey(to=Users,related_name='sort_user',on_delete=models.CASCADE,null=True,blank=True)
      STATUS_TYPE = (
             (0,"未处理"),
             (1,"已处理")
      )
      status = models.IntegerField(verbose_name="垃圾袋状态",choices=STATUS_TYPE,default=0,help_text="垃圾袋状态")
      device = models.ForeignKey(to="InputLogModel",related_name="sort_device",on_delete=models.CASCADE,null=True,blank=True)
      sort_time = models.DateTimeField(verbose_name="分拣时间",auto_now_add=True,null=True,blank=True)
    #   weight = models.CharField(verbose_name="分拣重量",max_length=255)
    #   real_weight = models.CharField(verbose_name="分拣复称重量",max_length=255)
    #   error_weight = models.CharField(verbose_name="异常分拣重量",max_length=255)
      class Meta:
                db_table = "sort"
                verbose_name = "分拣记录"
                verbose_name_plural = verbose_name
                ordering = ["-create_datetime"]

# app更新记录
class AppUpdateModel(CoreModel):
       url = models.CharField(verbose_name="apk链接",max_length=255)
       version = models.CharField(verbose_name="apk版本号",max_length=255)
       version_code = models.CharField(verbose_name="apk版本",max_length=255)
       message = models.CharField(verbose_name="更新信息",max_length=255,null=True,blank=True)
       class Meta:
              db_table = "app_version"
              verbose_name = "app更新记录"
              verbose_name_plural = verbose_name
              ordering = ["-create_datetime"]

class ImageModel(CoreModel):
       url = models.CharField(verbose_name="图片链接",max_length=255)
       clean = models.ForeignKey(to="CleanLogModel",related_name="image_clean",on_delete=models.CASCADE,null=True,blank=True)
       sort = models.ForeignKey(to="SortModel",related_name="image_sort",on_delete=models.CASCADE,null=True,blank=True)
       input = models.ForeignKey(to="InputLogModel",related_name="image_input",on_delete=models.CASCADE,blank=True,null=True)
       class Meta:
              db_table = "image"
              verbose_name = "图片链接"
              verbose_name_plural = verbose_name
              ordering = ["-create_datetime"]

# 用户投放垃圾记录
class WeChatUserInputAndMqtt(CoreModel):
    user = models.ForeignKey(to="WechatUserModel",related_name='input_user',on_delete=models.CASCADE)
    device = models.ForeignKey(to="DeviceModel",related_name='input_device',on_delete=models.CASCADE)
    class Meta:
          db_table = "mqtt_login"
          verbose_name = "Mqtt开箱记录"
          verbose_name_plural = verbose_name
          ordering = ["-create_datetime"]

class CleanerOpenBox(CoreModel):
    user = models.ForeignKey(to=Users,related_name='cleaner_open_box',on_delete=models.CASCADE,null=True,blank=True)
    device = models.ForeignKey(to="DeviceModel",related_name='cleaner_device',on_delete=models.CASCADE)
    class Meta:
          db_table = "cleaner_open_box"
          verbose_name = "远程开箱记录"
          verbose_name_plural = verbose_name
          ordering = ["-create_datetime"]

# qiniu 图片上传
class QiniuImageUpload(CoreModel):
    access_key = models.CharField(verbose_name="ak",max_length=255)
    secret_key = models.CharField(verbose_name="sk",max_length=255)
    class Meta:
           db_table = "qiniu"
           verbose_name = "七牛"
           verbose_name_plural = verbose_name
           ordering = ["-create_datetime"]